package controller.KeyListeners;

import java.awt.event.KeyAdapter;

import view.GameWindow;
import view.mainscreen.AreaViewport;
import controller.Controller;
import controller.KeyConfig;
import model.command.*;

public class KeyListenerSet {
	KeyboardListener[] KLSet;
        private Controller controller;
        private boolean locked;
        private int state[];
        private KeyConfig config;
    public KeyListenerSet(Controller controller){
        setController(controller);
        config = new KeyConfig();
		KLSet = new KeyboardListener[7];
		KLSet[0] = new CursorKeyListener();
		KLSet[1] = new CycleKeyListener(controller);
		KLSet[2] = new DirectionalKeyListener(controller, this);
		KLSet[3] = new EnterKeyListener(controller);
		KLSet[4] = new NumberKeyListener(controller);
		KLSet[5] = new SelectionKeyListener(controller);
		KLSet[6] = new ViewChangeKeyListener();
	}
        /**
         * This is new. Allows the command to choose which listener to
         * attach to.
         *
         * @param command
         */
        public void attachToDirectionListener(DirectionalCommand command){
           ((DirectionalKeyListener) KLSet[2]).setCommand(command);
           int activate[] = {1, 2, 5, 6};
           registerKLSet(activate);
        }
        /**
         *
         * 
         * @param command
         */
        public void attachToEnterListener(Command command){
            ((EnterKeyListener) KLSet[3]).setCommand(command);
            int activate[] = {0, 1, 3, 5, 6};
            registerKLSet(activate);
        }
        /**
         * This is new. Allows the command to choose which listener to
         * attach to.
         *
         * @param command
         */
        public void attachToNumberListener(NumberCommand command){
            ((NumberKeyListener) KLSet[4]).setCommand(command);
            int active[] = {0, 1, 4, 6};
            registerKLSet(active);
        }
        public void attachToViewChangeKeyListener(GameWindow gameWindow){
        	((ViewChangeKeyListener) KLSet[6]).attachGameWindow(gameWindow);
        }
        public void attachToCursorKeyListener(AreaViewport areaViewport){
        	((CursorKeyListener) KLSet[0]).attachAreaViewport(areaViewport);
        }
        /**
         * This is new. It will allow the cycle keys to be deactivated once
         * the move directions are input.
         * 
         */
        public void lock(int unlocked[]){
            locked = true;
            registerKLSet(unlocked);
        }
        /**
         * This is new. It determines if the key listener set is locked.
         *
         * @return
         */
        public boolean isLocked(){
            return locked;
        }
	public void registerKLSet(int[] _KLSet){
                if(!locked) state = _KLSet;
		//Deactive old set
		for(int i= 0; i < KLSet.length;i++){
			KLSet[i].deactivate();
		}
		//Active new set
		for(int i= 0; i < _KLSet.length;i++){
			if(_KLSet[i] >= 0 && _KLSet[i] < KLSet.length){
				KLSet[_KLSet[i]].activate();
				System.out.println("\t\t\t" + KLSet[_KLSet[i]].toString());
			}
		}
	}
        private void setController(Controller controller){
            this.controller = controller;
        }
        /**
         * This is new. It unlocks the cycle keys when the move command is finished.
         */
        public void unlock(){
            locked = false;
            registerKLSet(state);
        }
	public KeyboardListener[] getKLSet(){
		return KLSet;
	}
}
